import cityView from '../views/city.art'

import cityModel from '../models/city'


class City{
    constructor(){
        this.list = [];
        this.letter = []
    }
    renderer({list,citypy}){
        let cityListHtml = cityView({
            list,
            citypy
        })
        $('#root').html(cityListHtml)
    }
    async render(){
        let result = await cityModel.get({})
        let list = this.list = result.cts;
        let obj={}
        for(let i=97;i<123;i++){
            obj[String.fromCharCode(i)]=[];
            for(let j in list){
                if((list[j].py).substr(0,1)==String.fromCharCode(i)){
                    obj[String.fromCharCode(i)].push(list[j]);
                }
            }
        }
        this.renderer({
            list,
            citypy:obj
        })

        $('#city .hot-city .bg2 button').on('tap',function(){
            window.history.back()
            localStorage.setItem('cityHistory',this.innerHTML)
            let arr = []
            for(let i in list){
                arr.push([list[i].nm,list[i].id])
            }
            for(let j in arr){
                if(this.innerHTML == arr[j][0]){
                    localStorage.setItem('cityId',arr[j][1])
                }
            }
        })

        $('#city .city-list li').on('tap',function(){
            window.history.back()
            localStorage.setItem('cityHistory',this.innerHTML)
            let arr = []
            for(let i in list){
                arr.push([list[i].nm,list[i].id])
            }
            for(let j in arr){
                if(this.innerHTML == arr[j][0]){
                    localStorage.setItem('cityId',arr[j][1])
                }
            }
            
        })

        $('#city .point li').on('tap',function(){
            document.querySelector('#root').scrollTop=$('#'+$(this).html()).position().top;
        })


    }
}

export default new City()